package com.jilian.mydemo.modul.my.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.OrderDetailDto;
import com.jilian.mydemo.common.dto.PerformDto;
import com.jilian.mydemo.common.vo.ProductVO;
import com.jilian.mydemo.listener.CustomItemClickListener;
import com.jilian.mydemo.modul.order.adapter.ProductAdapter;
import com.jilian.mydemo.modul.order.viewmodel.OrderViewModel;
import com.jilian.mydemo.utils.DisplayUtil;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.jilian.mydemo.views.CustomLinearLayoutManager;
import com.jilian.mydemo.views.CustomerItemDecoration;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 水工业绩详情
 */
public class HydraulicPerformanceDetailActivity extends BaseActivity implements CustomItemClickListener {
    private TextView tvOrderNo;
    private TextView tvServiceType;
    private TextView tvEqCount;
    private RecyclerView recyclerView;
    private TextView tvAppointmentTime;
    private TextView etContent;
    private TextView etDemandContent;
    private TextView tvAccessoriesFee;
    private TextView tvServiceFee;
    private TextView tvPreferential;
    private TextView tvThisCard;


    private CustomLinearLayoutManager linearLayoutManager;
    private ProductAdapter adapter;

    private List<ProductVO> datas;


    private OrderViewModel orderViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }


    @Override
    protected void createViewModel() {
        orderViewModel = ViewModelProviders.of(this).get(OrderViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_hydraulicperformancedetail;
    }

    @Override
    public void initView() {
        setNormalTitle("详情", v -> finish());
        tvOrderNo = (TextView) findViewById(R.id.tv_order_no);
        tvServiceType = (TextView) findViewById(R.id.tv_service_type);
        tvEqCount = (TextView) findViewById(R.id.tv_eq_count);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        tvAppointmentTime = (TextView) findViewById(R.id.tv_appointment_time);
        etContent = (TextView) findViewById(R.id.et_content);
        etDemandContent = (TextView) findViewById(R.id.et_demand_content);
        tvAccessoriesFee = (TextView) findViewById(R.id.tv_accessories_fee);
        tvServiceFee = (TextView) findViewById(R.id.tv_service_fee);
        tvPreferential = (TextView) findViewById(R.id.tv_preferential);
        tvThisCard = (TextView) findViewById(R.id.tv_this_card);


        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.addItemDecoration(new CustomerItemDecoration(DisplayUtil.dip2px(this, 10)));
        linearLayoutManager = new CustomLinearLayoutManager(this);
        linearLayoutManager.setScrollEnabled(false);
        recyclerView.setLayoutManager(linearLayoutManager);
        datas = new ArrayList<>();
        adapter = new ProductAdapter(this, datas, this);
        recyclerView.setAdapter(adapter);
    }

    private PerformDto mData;

    @Override
    public void initData() {
        mData = (PerformDto) getIntent().getSerializableExtra("data");
        getOrderDetail();
    }

    /**
     * 获取订单详情
     */
    private void getOrderDetail() {
        showLoadingDialog();
        orderViewModel.queryDetail(mData.getId());
        orderViewModel.getDetailliveData().observe(this, new Observer<BaseDto<OrderDetailDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<OrderDetailDto> dto) {
                hideLoadingDialog();
                if (dto.isSuccess()) {
                    if (EmptyUtils.isNotEmpty(dto)) {
                        initDetailView(dto.getData());
                    }
                } else {
                    ToastUitl.showImageToastTips(dto.getMsg());
                }

            }
        });
    }

    /**
     * 展示订单详情
     *
     * @param data
     */
    private void initDetailView(OrderDetailDto data) {
        //订单号
        tvOrderNo.setText(data.getOrderNo());
        //服务类型
        tvServiceType.setText(data.getServiceType());
        //设备数量
        tvEqCount.setText(data.getDeviceAmount() + "台");
        //服務產品 為列表
        datas.clear();
        datas.addAll(data.getProductVOS());
        adapter.notifyDataSetChanged();
        //预约时间
        tvAppointmentTime.setText(data.getBookTime());
        //语预约说明
        etContent.setText(data.getBookRemark());
        //服务需求
        etDemandContent.setText(data.getServiceRemark());
        //配件费 保留两位
        tvAccessoriesFee.setText(data.getPartsMoney() == null ? "0.00" : data.getPartsMoney().setScale(2, BigDecimal.ROUND_DOWN).toString());
        //服务费
        tvServiceFee.setText(data.getServiceMoney() == null ? "0.00" : data.getServiceMoney().setScale(2, BigDecimal.ROUND_DOWN).toString());
        //优惠
        tvPreferential.setText(data.getReduceMoney());
        //挂账
        tvThisCard.setText(data.getPendingMoney() == null ? "0.00" : data.getPendingMoney().setScale(2, BigDecimal.ROUND_DOWN).toString());
    }

    @Override
    public void initListener() {

    }

    @Override
    public void onItemClick(View view, int position) {

    }
}
